package com.linb.pan.storage.engine.oss.config;

import com.aliyun.oss.OSSClient;
import com.linb.pan.core.exception.BPanBusinessException;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

@Data
@Component
@ConfigurationProperties(prefix = "com.linb.pan.storage.engine.oss")
public class OssStorageEngineConfig {

    private String endPoint;

    private String accessKeyId;

    private String accessKeySecret;

    private String bucketName;

    private Boolean autoCreateBucket = Boolean.FALSE;

    @Bean(destroyMethod = "shutdown")
    public OSSClient ossClient() {
        if (StringUtils.isAnyBlank(getEndPoint(), getAccessKeyId(), getAccessKeySecret(), getBucketName())) {
            throw new BPanBusinessException("oss配置有误");
        }
        return new OSSClient(getEndPoint(), getAccessKeyId(), getAccessKeySecret());
    }

}
